<script setup>
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";
import dayjs from "dayjs";
import {useRouter} from "vue-router";

/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openView,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.post,
  params: {
    name: '',
  },
  format: (res) => {
    return res.map(el => {
      let comments = (el.comments || []).map(el2 => {
        let user = el2.user || {}
        if (user.type === 0) {
          user.typeName = '普通用户'
        } else if (user.type === 1) {
          user.typeName = '律师'
        } else if (user.type === 2) {
          user.typeName = '管理员'
        }
        return {
          ...el2,
          create_time: el2.create_time ? dayjs(el2.create_time).format('YYYY-MM-DD HH:mm:ss') : undefined,
          user
        }
      })
      let user = el.user || {}
      if (user.type === 0) {
        user.typeName = '普通用户'
      } else if (user.type === 1) {
        user.typeName = '律师'
      } else if (user.type === 2) {
        user.typeName = '管理员'
      }
      return {
        ...el,
        user,
        content: (el.content || '').replace(/<[^>]+>/g,""),
        create_time: el.create_time ? dayjs(el.create_time).format('YYYY-MM-DD HH:mm:ss') : undefined,
        comments:comments,
      }
    })
  },
})
function openUpdate(row) {
  http.post.update({
    id: row.id,
    status: row.status ? 0 : 1
  }).then(res => {
    search()
  })
}
const router = useRouter()
function openComment(row) {
  router.push({
    path: '/postAdmin/comment/' + row.id,
  })
}
</script>


<template>
  <PageMain title="帖子管理">
    <el-form label-suffix="：" inline>
      <el-form-item label="名称">
        <el-input v-model="searchParams.name" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="名称" prop="name">
        </el-table-column>
        <el-table-column label="图片">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <ViewImage type="image" :image-url="scope.row.image"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="内容" prop="content">
        </el-table-column>
        <el-table-column label="发布者" prop="">
          <template #default="scope">
          {{ scope.row.user.nickname }}【{{scope.row.user.typeName}}】
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <div style="display: flex; align-items: center">
             <el-tag :type="scope.row.status ? 'success' : 'info'">{{ scope.row.status ? '上架' : '下架'}}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220px" fixed="right">
          <template #default="scope">
            <el-button type="text" @click="openUpdate(scope.row)">{{ scope.row.status ? '下架' : '上架'}}</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
            <el-button type="text" @click="openComment(scope.row)">查看评论</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
